package demo.day11_CollectionAddpackage.List;

import java.util.*;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/2-08-02-19:41
 * @Description：demo.day11_CollectionAddpackage.List
 */
/*
编写一个学生管理类，可以添加学生（学号要有唯一性）、删除学生、查看学生信息，使用List集合来实现
 */
public class work3 {
    public static void main(String[] args) {
        Student_Control manger = new Student_Control();
        manger.Add(new student(1,"hhh"));
        manger.Add(new student(2,"hjy"));
        manger.Add(new student(3,"her"));
        manger.Add(new student(4,"tom"));
        manger.Add(new student(1,"aim"));
        manger.showAll();
        System.out.println("-------------------------------");
        manger.remove(2);
        manger.remove(5);
        System.out.println("--------------------------");
        manger.showAll();
    }
}
class student{
    private int id;       //学号
    private String name;     //姓名

    @Override
    public String toString() {
        return "student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        student student = (student) o;
        return id == student.id && name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public student() {
    }

    public student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
class Student_Control{
    static List<student>list = new ArrayList();
    //添加学生
    public void Add(student s){
        for (student stu: list) {
            if(stu.getId() == s.getId()) {
                System.out.println("id = " + s.getId() + "该学号已存在，添加失败！");
                return;
            }
        }
        list.add(s);
    }
    //删除学生
    public void remove( int id){
        student stu = null;
        for (Object o : list) {
            student stu2 = (student) o;
            if(stu2.getId() == id){
                stu = stu2;
                list.remove(o);
                break;
            }
        }
        if(stu == null){
            System.out.println("未查找到学生id = " + id + "的信息，删除失败！");
        }else{
            System.out.println("删除成功！" + stu + " 该学生信息已删除！");
        }
    }
    //查看学生信息
    public void showAll(){
        for (Object o : list) {
            student stu = (student) o;
            System.out.println(stu);
        }
    }
}